package com.icss.ah.base.action;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import com.icss.ah.auditGoods.service.AuditGoodsService;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.bean.WxCorpInfoBean;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.categoryProduct.service.CategoryProductService;
import com.icss.ah.clerkAccount.service.ClerkAccountService;
import com.icss.ah.custMsg.service.CustMessageService;
import com.icss.ah.delivery.service.DeliveryAddressInfoService;
import com.icss.ah.designatedSales.service.DesignatedSalesService;
import com.icss.ah.distributionPerson.service.DistributionPersonService;
import com.icss.ah.email.service.EmailAuthenticateService;
import com.icss.ah.expressBird.service.ExpressBirdService;
import com.icss.ah.fullReduce.service.FullStatisticsService;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.login.service.LoginService;
import com.icss.ah.login.service.MenuRightService;
import com.icss.ah.login.service.TipsService;
import com.icss.ah.orderFee.service.OrderFeeService;
import com.icss.ah.productForManager.service.ProductForManagerService;
import com.icss.ah.productForShow.service.ProductForShowService;
import com.icss.ah.productGroup.service.ProductGroupService;
import com.icss.ah.shop.service.JfShoppingService;
import com.icss.ah.shop.service.ShopApplyService;
import com.icss.ah.shop.service.ShopDateSellService;
import com.icss.ah.shop.service.ShopManageService;
import com.icss.ah.shopVipSet.service.ShopVipInfoService;
import com.icss.ah.shopVipSet.service.ShopVipSetService;
import com.icss.ah.storeApply.service.StoreBindService;
import com.icss.ah.test.service.TestService;
import com.icss.ah.vehicleInfo.service.VehicleInfoService;
import com.icss.ah.vehicleUsers.service.VehicleUsersService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * context,session,request,response
 * 
 * @author: chensheng
 */
public abstract class BaseAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	protected int page = 1;// 当前页
	protected int rows = 10;// 每页显示记录数
	protected String appId = ResourceLoader.getInstance("icss").getString("appId"); //项目（应用）id
	protected String TMP_ORDER_URL = ResourceLoader.getInstance("icss").getString("TMP_ORDER_URL"); //订单详情链接
	protected String TMP_DISPATCHER_URL = ResourceLoader.getInstance("icss").getString("TMP_DISPATCHER_URL"); //送货任务链接
	protected String icssMchCode = ResourceLoader.getInstance("icss").getString("icssMchCode"); //支付商户号
	protected String sort = null;// 排序字段名
	protected String order = "asc";// 按什么排序(asc,desc)
	protected Json jsonText;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Map<String,Object> session;
	protected EasyuiDataGridJson dataGridJson = new EasyuiDataGridJson(); //页面返回表格的信息
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	@Autowired
	protected TestService testService;
	@Autowired
	protected LoginService loginService;
	@Autowired
	protected TipsService tipsService;
	@Autowired
	protected MenuRightService menuRightService;
	@Autowired
	protected ShopApplyService shopService;
	@Autowired
	protected ShopManageService shopManageService;
	@Autowired
	protected ShopVipSetService shopVipSetService;
	@Autowired
	protected EmailAuthenticateService emailAuthenticateService;
	@Autowired
	protected CustMessageService custMessageService;
	@Autowired
	protected FullStatisticsService fullStatisticsService;
	
	@Autowired
	protected ShopVipInfoService shopVipInfoService;
	@Autowired
	protected ShopDateSellService shopDateSellService;
	@Autowired
	protected JfShoppingService jfShoppingService;
	@Autowired
	protected CategoryProductService categoryProductService;
	@Autowired
	protected ProductForManagerService productForManagerService;
	@Autowired
	protected ProductForShowService productForShowService;
	@Autowired
	protected DeliveryAddressInfoService deliveryAddressInfoService;
	@Autowired
	protected AuditGoodsService auditGoodsService;
	@Autowired
	protected ClerkAccountService clerkAccountService;
	@Autowired
	protected DistributionPersonService distributionPersonService;
	@Autowired
	protected DesignatedSalesService designatedSalesService;
	@Autowired
	protected ProductGroupService productGroupService;
	@Autowired
	protected VehicleInfoService vehicleInfoService;
	@Autowired
	protected VehicleUsersService vehicleUsersService;
	@Autowired
	protected OrderFeeService orderFeeService;
	@Autowired
	protected ExpressBirdService expressBirdService;
	@Autowired
	protected StoreBindService storeBindService;
	
	public String getStartIndexMysql(){
		if(page<1) page=1;
		return String.valueOf((page - 1) * rows);
	} 
	public String getEndIndexMysql(){
		return String.valueOf(rows);
	}
	public ActionContext getActionContext() {
		return ActionContext.getContext();
	}

	public Map getSession() {
		return getActionContext().getSession();
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	/**
	 * outjson
	 * @return 
	 */
	public void str2Resp(String str) {
		try {
			getResponse().setContentType("text/html;charset=utf-8");
			PrintWriter out = getResponse().getWriter();
			out.print(str);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	/**
	 * 从session中获取userCode
	 * @return
	 */
	public String getUserCodeFromSession(){
		String userCode = getRequest().getSession().getAttribute("userCode")== null ? "" : getRequest().getSession().getAttribute("userCode").toString();
		return userCode;
	}
	/**
	 * 获取登录用户信息
	 * @param userCode
	 * @return
	 */
	public UserInfoBean getUserInfo(String userCode){
		userCode = userCode == null||userCode.equals("") ? getUserCodeFromSession(): userCode;
		UserInfoBean userInfo = (UserInfoBean)getRequest().getSession().getAttribute(userCode+"UserInfoBean");
		if(userInfo==null){
			userInfo = loginService.getUserInfo(userCode);
			getRequest().getSession().setAttribute(userCode+"UserInfoBean", userInfo);
		}
		if(userInfo.getCorpId()==null||userInfo.getCorpId().equals("")){
			String corpId = ResourceLoader.getInstance("icss").getString("corpId");
			userInfo.setCorpId(corpId);
		}
		return userInfo;
	}
	/**
	 * 获取登录用户信息-不从session获取
	 * @param userCode
	 * @return
	 */
	public UserInfoBean getUserInfoNoSession(String userCode){
		userCode = userCode == null||userCode.equals("") ? getUserCodeFromSession(): userCode;
		UserInfoBean userInfo = loginService.getUserInfo(userCode);
		getRequest().getSession().setAttribute(userCode+"UserInfoBean", userInfo);
		if(userInfo.getCorpId()==null||userInfo.getCorpId().equals("")){
			String corpId = ResourceLoader.getInstance("icss").getString("corpId");
			userInfo.setCorpId(corpId);
		}
		return userInfo;
	}
	/**
	 * session清理
	 * @param userCode
	 */
	public void removeUserInfo(String userCode){
		getRequest().getSession().removeAttribute(userCode+"UserInfoBean");
		getRequest().getSession().removeAttribute("userCode");
		getRequest().getSession().removeAttribute("password");
	}
	//获取corpId
	public String getBaseCorpId(){
		UserInfoBean userInfo = getUserInfo("");
		return userInfo.getCorpId();
	}
	/**
	 * 根据corpId获取公众号信息
	 * @param corpId
	 * @return
	 */
	public WxCorpInfoBean getWxCorpInfoBeanByCorpId(String corpId){
		WxCorpInfoBean corpInfo = null;
		try{
			corpInfo = (WxCorpInfoBean)getRequest().getSession().getAttribute(corpId+"WxCorpInfoBean");
			if(corpInfo==null){
				HashMap hm = new HashMap();
				hm.put("corpId", corpId);
				corpInfo = loginService.getWxCorpInfoBeanByCorpId(hm);
				getRequest().getSession().setAttribute(corpId+"WxCorpInfoBean", corpInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return corpInfo;
	}
	/**
	 * 获取模版id(templateId)
	 */
	public String getTemlateIdByCorp(String corpId,String templateType){
		String templateId = "";
		try {
			HashMap query = new HashMap();
			query.put("corpId", corpId);
			query.put("templateType", templateType);
			templateId = sqlSessionTemplate.selectOne("LogisticBean.getTemlateIdByCorp",query);
			templateId = templateId == null ? "" : templateId;
		} catch (RuntimeException e) {
			e.printStackTrace();
			templateId = "";
		}
		return templateId;
	}
	
	/**
	 * 获取电商加密私钥
	 */
	public String getAppKey(){
		String appKey = "";
		try {
			String corpId = getBaseCorpId();
			appKey = sqlSessionTemplate.selectOne("LogisticBean.getAppKey",corpId);
			appKey = appKey == null ? "" : appKey;
		} catch (Exception e) {
			e.printStackTrace();
			appKey = "";
		}
		return appKey;
	}
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public Json getJsonText() {
		return jsonText;
	}

	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public EasyuiDataGridJson getDataGridJson() {
		return dataGridJson;
	}

	public void setDataGridJson(EasyuiDataGridJson dataGridJson) {
		this.dataGridJson = dataGridJson;
	}

}
